home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Celestin Apprentice 5
/
Apprentice-Release5.iso
/
Source Code
/
Libraries
/
Simple Help
/
TestHelp.c
< prev
Wrap
C/C++ Source or Header
|
1994-10-13
|
13KB
|
590 lines
/************************************************************************************************
*
*
* ColourLabMain.c - Main program for ColourLab application
*
*
* 15/7/94 ©1994 by Graham Cox
*
*
*************************************************************************************************/
#include "ColourLabHeaders.h"
Boolean gDone = FALSE;
short gSleepTime;
WindowPtr gColourPalette;
WindowMenuHdl gPalettesList = NULL;
OSType gCreatorType = 'gCox';
SimHelpHdl gHelp = NULL;
WindowPtr gCalWindow = NULL;
WindowPtr gCustomToolPalette = NULL;
Boolean gIsInBackground = FALSE;
EventRecord gCurrentEvent;
wVisStatusHdl gFloaterStatus = NULL;
void main(void)
{
EventRecord theEvent;
short wneLoopCount;
StdMacInit(8); // standard initialisation code
ColourLabInit(); // init this app
// for power PC, we should fetch events less frequently for better performance. This ensures that
wneLoopCount = kWNEFetchDuty;
while (! gDone)
{
if (wneLoopCount >= kWNEFetchDuty)
{
wneLoopCount = 0;
if (WaitNextEvent(everyEvent,&theEvent,gSleepTime,NULL))
HandleOneEvent(&theEvent);
else
SSRun(gSlideShow);
}
else
{
wneLoopCount++;
// this is where idle tasks, etc should be inserted
StopCursorAnimation();
AdjustImageCursor(TopDocument());
AnimateSelection(TopDocument());
}
}
VideoCleanUp();
//CleanUpGXPrinting();
}
void HandleOneEvent(EventRecord *theEvent)
{
WindowPtr targWindow;
char keyChar;
long mSelect;
OSErr theErr;
MenuHandle sampleMenu;
gCurrentEvent = *theEvent;
switch (theEvent->what)
{
case mouseDown:
HandleMouseEvent(theEvent);
break;
case updateEvt:
targWindow = (WindowPtr) theEvent->message;
SetPort(targWindow);
BeginUpdate(targWindow);
// draw something
if (Is3DKind(targWindow))
DrawTypicalWindow(targWindow);
if (IsColoursPalette(targWindow))
DrawColoursPalette(targWindow);
if (IsCaliperWindow(targWindow))
DrawCaliperWindow(targWindow);
if (IsToolWindow(targWindow))
UpdateTools(targWindow);
if (targWindow == gSlideShowPalette)
UpdateSlideInfo(gSlideShow);
EndUpdate(targWindow);
break;
case keyDown:
keyChar = theEvent->message & charCodeMask;
if ((theEvent->modifiers & cmdKey) == cmdKey)
{
AdjustMenus();
sampleMenu = GetMHandle(kImageMenu);
mSelect = PowerMenuKey(theEvent->message,theEvent->modifiers,sampleMenu);
HandleMenuSelection(mSelect);
}
else
HandleKeypress(TopDocument(),keyChar);
break;
case autoKey:
keyChar = theEvent->message & charCodeMask;
HandleKeypress(TopDocument(),keyChar);
break;
case activateEvt:
targWindow = (WindowPtr) theEvent->message;
SetPort(targWindow);
Activate3DWindow(targWindow,theEvent->modifiers & activeFlag);
break;
case diskEvt:
// don't bother to handle this one- SF or the Finder will get it eventually
break;
case osEvt:
HandleOSEvent(theEvent);
break;
case kHighLevelEvent:
theErr = AEProcessAppleEvent(theEvent);
break;
}
}
SimHelpHdl OpenHelp()
{
SimHelpHdl sHelp;
DialogPtr hDlog;
short itemType;
Handle itemHand;
Rect itemBox;
GrafPtr savePort;
static UserItemUPP my3DItem;
sHelp = GetNewHelpDialog(kHelpDialogID,kListUserItem,kTextUserItem,kTopicResID);
if (sHelp != NIL)
{
hDlog = GetHelpDialogHdl(sHelp);
HideDItem(hDlog,ok);
InstallColourUserItem(hDlog,kColourPopUpItem);
GetDItem(hDlog,k3DAdornmentItem,&itemType,&itemHand,&itemBox);
if ((itemType & 0x7F) == userItem)
{
my3DItem = NewUserItemProc((ProcPtr) ThreeDDrawProc);
itemHand = (Handle) my3DItem;
SetDItem(hDlog,k3DAdornmentItem,itemType,itemHand,&itemBox);
GetDItem(hDlog,k3DAdornmentItem - 1,&itemType,&itemHand,&itemBox);
itemHand = (Handle) my3DItem;
SetDItem(hDlog,k3DAdornmentItem - 1,itemType,itemHand,&itemBox);
}
RegisterNewWindow(gWMList,(WindowPtr) hDlog);
ShowFWWindow(hDlog);
SelectFWWindow(hDlog);
}
return(sHelp);
}
#ifndef __CURSORUTILITIES__
void SetWatchCursor(void)
{
CursHandle watch;
watch = GetCursor(watchCursor);
SetCursor(*watch);
}
#endif
void HandleMouseEvent(EventRecord *theEvent)
{
/* This is called when the event retrieved was a mouseDown */
short targLoc,newJawWidth;
WindowPtr targWindow;
Rect dragBounds,growBounds;
long mSelect,growFactor;
RGBColor pickColour;
CalcIdealDocumentSizeUPP theCalcProc;
targLoc = FindWindow(theEvent->where,&targWindow);
dragBounds = (*GetGrayRgn())->rgnBBox;
InsetRect(&dragBounds,4,4);
switch (targLoc)
{
case inMenuBar:
CheckWindowInMenu(gWMList,TopDocument());
CheckWindowInMenu(gPalettesList,FirstVisibleFloater());
AdjustMenus();
mSelect = MenuSelect(theEvent->where);
HandleMenuSelection(mSelect);
break;
case inDrag:
DragFWWindow(targWindow,theEvent->where,&dragBounds);
break;
case inGrow:
if (! IsFloating(targWindow))
{
SetRect(&growBounds,kMinWindowWidth,kMinWindowHeight,dragBounds.right,dragBounds.bottom);
growFactor = GrowWindow(targWindow,theEvent->where,&growBounds);
if (growFactor)
{
SetPort(targWindow);
if (Is3DKind(targWindow))
Resize3DWindow(targWindow,LoWord(growFactor),HiWord(growFactor),FALSE);
else
SizeWindow(targWindow,LoWord(growFactor),HiWord(growFactor),FALSE);
}
}
else
{
if (IsToolWindow(targWindow))
{
mSelect = ClickInToolWindow(targWindow,theEvent);
SelectTool(TopDocument(),mSelect);
}
}
break;
case inGoAway:
if(TrackGoAway(targWindow,theEvent->where))
{
CloseTheWindow(targWindow);
CalcWindowPointers();
}
break;
case inZoomIn:
case inZoomOut:
if(TrackBox(targWindow,theEvent->where,targLoc))
{
theCalcProc = NewCalcIdealDocumentSizeProc(CalcOptimumZoomSize);
ZoomTheWindow((WindowPeek) targWindow,targLoc,theCalcProc);
DisposeRoutineDescriptor(theCalcProc);
}
break;
case inSysWindow:
SystemClick(theEvent,targWindow);
break;
case inContent:
if (targWindow != TopDocument())
{
SelectFWWindow(targWindow);
if (IsToolWindow(targWindow))
{
mSelect = ClickInToolWindow(targWindow,theEvent);
SelectTool(TopDocument(),mSelect);
}
if (IsColoursPalette(targWindow))
{
FindColourClicked(targWindow,theEvent->where,&pickColour);
if (theEvent->modifiers & optionKey)
(*gPaintInfo)->lineColour = pickColour;
else
(*gPaintInfo)->fillColour = pickColour;
}
}
else
// handle mouse clicks in the window here
Click3DWindow(targWindow,theEvent->where,theEvent->modifiers);
break;
case wInCaliperThumb + 2:
// this must be the caliper window- no other WDEF could return this value
newJawWidth = DragCaliperThumb(targWindow,theEvent->where);
SetNewJawWidth(targWindow,newJawWidth);
break;
default:
SysBeep(1);
break;
}
}
void TranslateGXMenuItem(short menuID,short *itemID)
{
// if GX isn't present, this ensures that the menu item is correct for the menu dispatcher even if
// the menu has been modified
if (menuID == kFileMenu && ! gGxIsPresent)
{
if (*itemID == kItemCustomPageSetup)
*itemID = kItemPrint;
else
if (*itemID == kItemPrintOne)
*itemID = kItemQuit;
}
}
#define kAboutBoxAlertID 256
void DisplayAboutBox(void)
{
/* Display an alert which gives some info about this app */
short aHit;
ParamText("\pJohn Lennon","\pThe Beatles",NULL,NULL);
aHit = Alert(kAboutBoxAlertID,NULL);
}
pascal void ThreeDDrawProc(WindowPtr theWindow,short theItem)
{
/* This drawproc implements some non-vital 3D shading effects for a useritem */
short itemType;
Handle itemHand;
Rect itemBox;
GetDItem(theWindow,theItem,&itemType,&itemHand,&itemBox);
Frame3DRect(&itemBox,FALSE);
// Also create 3D effect for the window as a whole
itemBox = qd.thePort->portRect;
InsetRect(&itemBox,1,1);
Frame3DRect(&itemBox,TRUE);
}
void SetEditableHelp(SimHelpHdl theHelp,Boolean isEditable)
{
TDRecHdl tPanelHdl;
DialogPtr hDlog;
if(theHelp)
{
hDlog = GetHelpDialogHdl(theHelp);
tPanelHdl = (TDRecHdl) GetWRefCon(hDlog);
if (tPanelHdl)
(*tPanelHdl)->editable = isEditable;
if (isEditable)
{
TEAutoView(TRUE,(*tPanelHdl)->TDText);
TEActivate((*tPanelHdl)->TDText);
}
else
{
TEDeactivate((*tPanelHdl)->TDText);
TEAutoView(FALSE,(*tPanelHdl)->TDText);
}
}
}
void CloseTheWindow(WindowPtr theWindow)
{
// closes the window. Action depends on type of window passed. If a document, the image status is
// checked and confirmed with user if save is needed
short iClose;
OSErr theErr;
GrafPtr savePort;
if (theWindow)
{
if (Is3DKind(theWindow))
{
iClose = ImageShouldBeSaved(theWindow,gDone);
switch (iClose)
{
default:
case kSaveTheImage:
theErr = SavePictureFile(theWindow,FALSE);
if (theErr == noErr)
{
UnregisterWindow(gWMList,theWindow);
Dispose3DWindow(theWindow);
ClearUndoBuffer(gUndoInfo);
}
else
SysBeep(1);
break;
case kDontSaveTheImage:
UnregisterWindow(gWMList,theWindow);
Dispose3DWindow(theWindow);
ClearUndoBuffer(gUndoInfo);
break;
case kCancelTheOperation:
if (gDone)
gDone = FALSE;
break;
}
if (gColourPalette)
{
GetPort(&savePort);
SetPort(gColourPalette);
InvalRect(&qd.thePort->portRect);
SetPort(savePort);
}
if (gSlideShow)
{
if ((*gSlideShow)->owner == theWindow)
(*gSlideShow)->owner = NULL;
}
}
else
{
if (! IsFloating(theWindow))
UnregisterWindow(gWMList,theWindow);
HideFWWindow(theWindow);
}
// recompute the floating stuff and send the right activate events
CalcWindowPointers();
ActivateDocuments(TopDocument(),NULL);
}
}
void CloseAllWindows()
{
WindowPeek temp,next;
temp = (WindowPeek) FrontWindow();
while(temp)
{
next = temp->nextWindow;
CloseTheWindow((WindowPtr) temp);
temp = next;
}
}
pascal OSErr OpenAppAE(AppleEvent *theAppleEvent,AppleEvent *reply,long refCon)
{
// apple event handler for the OpenApplication apple event message
HiliteMenu(kFileMenu);
HandleMenuSelection(((long) kFileMenu << 16) + kItemNew);
return(noErr);
}
pascal OSErr OpenDocsAE(AppleEvent *theAppleEvent,AppleEvent *reply,long refCon)
{
// apple event handler for the OpenDocuments apple event message. This extracts the document aliases
// from the event and opens the files passed
OSErr theErr;
FSSpec theFile;
long docCount,index;
Size actualSize;
AEDescList docList;
AEKeyword keyWord;
DescType returnedType;
theErr = AEGetParamDesc(theAppleEvent,keyDirectObject,typeAEList,&docList);
if (theErr == noErr)
{
theErr = AECountItems(&docList,&docCount);
if (! theErr)
{
for (index = 0;index < docCount;index++)
{
theErr = AEGetNthPtr( &docList,
index +1,
typeFSS,
&keyWord,
&returnedType,
&theFile,
sizeof(FSSpec),
&actualSize);
if (! theErr)
{
// open the file that we have been passed. We pass the buck on to another
// function, which checks the filetype, etc
HiliteMenu(kFileMenu);
(void) OpenPicFileFromSpec(&theFile,NULL);
HiliteMenu(0);
}
}
}
theErr = AEDisposeDesc(&docList);
}
return(theErr);
}
pascal OSErr PrintDocsAE(AppleEvent *theAppleEvent,AppleEvent *reply,long refCon)
{
// apple event handler for the PrintDocuments apple event message (TO DO)
return(noErr);
}
pascal OSErr QuitAppAE(AppleEvent *theAppleEvent,AppleEvent *reply,long refCon)
{
// apple event handler for the QuitApplication apple event message
gDone = TRUE;
RecordPalettePositions();
CloseAllWindows();
return(noErr);
}
void HandleOSEvent(EventRecord *theEvent)
{
// handles and dispatches os Events such as suspend and resume
Boolean suspend;
suspend = !(theEvent->message & 1);
if (suspend)
{
HLVideoControl(kVideoPaused,gVideoChannel,TopDocument());
// pause the slide show if it is running.
if (gSlideShow)
{
if ((*gSlideShow)->ssStatus == kSSRunning)
{
SlideShowControl(gSlideShow,kSSPauseShow);
SSRun(gSlideShow); // force it to stop now
}
}
gIsInBackground = TRUE;
gSleepTime = kBkgndSleepTime;
// we should hide all of our floating windows and clipboard. We need to
// record the viz status of the windows so that we can restore them later
gFloaterStatus = RecordWindowVisStatus();
ShowHideFloaters(kHideAllFloaters);
}
else
{
gIsInBackground = FALSE;
gSleepTime = kSleepTime;
// we should show the floating windows that were showing before
RestoreWindowVisStatus(gFloaterStatus);
gFloaterStatus = NULL;
HLVideoControl(kVideoPaused,gVideoChannel,TopDocument());
}
}
void RecordPalettePositions()
{
// saves all the palettes in the prefs file so that they reappear where the user put them last
short index = kPalPrefBaseID;
SaveWindowLocation(gColourPalette,index++,TRUE,FALSE);
SaveWindowLocation(gCalWindow,index++,TRUE,FALSE);
SaveWindowLocation(gCustomToolPalette,index++,TRUE,FALSE);
SaveWindowLocation(gSlideShowPalette,kSSWindowLocPrefID,TRUE,FALSE);
}
// Mein Hund hat kein Nase!
// Kein Nase? Wie riecht er?
// Schrecklich!!!